package com.example.demo.algorithm.linear;

import java.util.Iterator;

/**
 * @Author: xiatian
 * @CreateTime: 2021/9/14 10:30 上午
 * @Description: 链表
 */
public class SequenceList<T> implements Iterable<T> {
    //存储元素的数组
    private T[] storge;
    //当前链表的长度
    private int N;

    public SequenceList(int capacity) {
        storge = (T[]) new Object[capacity];
        N = 0;
    }

    /**
     * 清空线性表
     */
    public void clear() {
        N = 0;
    }

    /**
     * 判断线性表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return N == 0;
    }

    /**
     * 返回线性表的长度
     *
     * @return
     */
    public int length() {
        return N;
    }

    /**
     * 获取index处的元素T
     *
     * @param index
     * @return
     */
    public T get(int index) {
        if (index < 0 || index >= storge.length) {
            throw new RuntimeException("当前元素不存在");
        }
        return storge[index];
    }

    /**
     * 在线性表后面插入元素
     *
     * @param t
     */
    public void insert(T t) {
        //元素已经放满了数组，需要扩容
        if (N == storge.length) {
            resize(storge.length * 2);
        }
        storge[N++] = t;
    }

    /**
     * 在指定位置插入元素
     *
     * @param index
     * @param t
     */
    public void insert(int index, T t) {
        if (index < 0 || index >= N) {
            throw new RuntimeException("插入的位置不合法");
        }
        //元素已经放满了数组，需要扩容
        if (N == storge.length) {
            resize(storge.length * 2);
        }
        for (int i = N; i > index; i--) {
            storge[i] = storge[i - 1];
        }
        storge[index] = t;
        N++;
    }

    /**
     * 删除并返回线性表中第index个数据元素
     *
     * @param index
     * @return
     */
    public T remove(int index) {
        if (index < 0 || index > N - 1) {
            throw new RuntimeException("当前要删除的元素不存在");
        }
        T temp = storge[index];
        for (int i = index; i < N - 1; ++i) {
            storge[i] = storge[i + 1];
        }
        N--;

        //当元素已经不足数组大小的1/4,则重置数组的大小
        if (N > 0 && N < storge.length / 4) {
            resize(storge.length / 2);
        }


        return temp;
    }

    /**
     * 返回线性表中首次出现的指定的数据元素的位序号，若不存在，则返回-1
     *
     * @param t
     * @return
     */
    public int indexOf(T t) {
        if (t == null) {
            throw new RuntimeException("查找的元素不合法");
        }
        for (int i = 0; i < N; ++i) {
            if (storge[i].equals(t)) {
                return i;
            }
        }
        return -1;
    }

    private void resize(int newSize) {
        T[] temp = storge;
        storge = (T[]) new Object[newSize];
        for (int i = 0; i < temp.length; ++i) {
            storge[i] = temp[i];
        }
    }

    @Override
    public Iterator<T> iterator() {
        return new SIterator();
    }

    private class SIterator implements Iterator {
        private int cur;

        public SIterator() {
            this.cur = 0;
        }

        @Override
        public boolean hasNext() {
            return cur < N;
        }

        @Override
        public T next() {
            return storge[cur++];
        }
    }
}
